import { Model, DataTypes, Optional } from 'sequelize';
import sequelize from '@/config/database';

// 定义业务表属性接口
interface GenTableAttributes {
  tableId: number;
  tableName: string;
  tableComment: string;
  subTableName?: string;
  subTableFkName?: string;
  className: string;
  tplCategory: string;
  tplWebType?: string;
  packageName: string;
  moduleName: string;
  businessName: string;
  functionName: string;
  functionAuthor: string;
  genType?: string;
  genPath?: string;
  options?: string;
  treeCode?: string;
  treeParentCode?: string;
  treeName?: string;
  parentMenuId?: number;
  parentMenuName?: string;
  createBy?: string;
  createTime?: Date;
  updateBy?: string;
  updateTime?: Date;
  remark?: string;
}

// 创建时可选的字段
interface GenTableCreationAttributes extends Optional<GenTableAttributes, 'tableId'> {}

// 业务表模型类
class GenTable extends Model<GenTableAttributes, GenTableCreationAttributes> implements GenTableAttributes {
  public tableId!: number;
  public tableName!: string;
  public tableComment!: string;
  public subTableName?: string;
  public subTableFkName?: string;
  public className!: string;
  public tplCategory!: string;
  public tplWebType?: string;
  public packageName!: string;
  public moduleName!: string;
  public businessName!: string;
  public functionName!: string;
  public functionAuthor!: string;
  public genType?: string;
  public genPath?: string;
  public options?: string;
  public treeCode?: string;
  public treeParentCode?: string;
  public treeName?: string;
  public parentMenuId?: number;
  public parentMenuName?: string;
  public createBy?: string;
  public createTime?: Date;
  public updateBy?: string;
  public updateTime?: Date;
  public remark?: string;

  // 关联的列
  public columns?: any[];
  // 关联的子表
  public subTable?: GenTable;
  // 主键列
  public pkColumn?: any;

  // 时间戳
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;

  // 判断是否为树形结构
  public isTree(): boolean {
    return this.tplCategory === 'tree';
  }

  // 判断是否为主子表结构
  public isSub(): boolean {
    return this.tplCategory === 'sub';
  }

  // 判断是否为单表结构
  public isCrud(): boolean {
    return this.tplCategory === 'crud';
  }
}

GenTable.init(
  {
    tableId: {
      type: DataTypes.BIGINT,
      primaryKey: true,
      autoIncrement: true,
      field: 'table_id'
    },
    tableName: {
      type: DataTypes.STRING(200),
      allowNull: false,
      field: 'table_name'
    },
    tableComment: {
      type: DataTypes.STRING(500),
      field: 'table_comment'
    },
    subTableName: {
      type: DataTypes.STRING(200),
      field: 'sub_table_name'
    },
    subTableFkName: {
      type: DataTypes.STRING(200),
      field: 'sub_table_fk_name'
    },
    className: {
      type: DataTypes.STRING(100),
      field: 'class_name'
    },
    tplCategory: {
      type: DataTypes.STRING(20),
      field: 'tpl_category'
    },
    tplWebType: {
      type: DataTypes.STRING(20),
      field: 'tpl_web_type'
    },
    packageName: {
      type: DataTypes.STRING(100),
      field: 'package_name'
    },
    moduleName: {
      type: DataTypes.STRING(30),
      field: 'module_name'
    },
    businessName: {
      type: DataTypes.STRING(30),
      field: 'business_name'
    },
    functionName: {
      type: DataTypes.STRING(50),
      field: 'function_name'
    },
    functionAuthor: {
      type: DataTypes.STRING(50),
      field: 'function_author'
    },
    genType: {
      type: DataTypes.STRING(1),
      field: 'gen_type'
    },
    genPath: {
      type: DataTypes.STRING(200),
      field: 'gen_path'
    },
    options: {
      type: DataTypes.STRING(1000),
      field: 'options'
    },
    treeCode: {
      type: DataTypes.STRING(100),
      field: 'tree_code'
    },
    treeParentCode: {
      type: DataTypes.STRING(100),
      field: 'tree_parent_code'
    },
    treeName: {
      type: DataTypes.STRING(100),
      field: 'tree_name'
    },
    parentMenuId: {
      type: DataTypes.BIGINT,
      field: 'parent_menu_id'
    },
    parentMenuName: {
      type: DataTypes.STRING(50),
      field: 'parent_menu_name'
    },
    createBy: {
      type: DataTypes.STRING(64),
      field: 'create_by'
    },
    createTime: {
      type: DataTypes.DATE,
      field: 'create_time'
    },
    updateBy: {
      type: DataTypes.STRING(64),
      field: 'update_by'
    },
    updateTime: {
      type: DataTypes.DATE,
      field: 'update_time'
    },
    remark: {
      type: DataTypes.STRING(500)
    }
  },
  {
    sequelize,
    tableName: 'gen_table',
    timestamps: true,
    createdAt: 'createTime',
    updatedAt: 'updateTime',
    hooks: {
      beforeCreate: (genTable: GenTable) => {
        if (!genTable.createTime) {
          genTable.createTime = new Date();
        }
      },
      beforeUpdate: (genTable: GenTable) => {
        genTable.updateTime = new Date();
      }
    }
  }
);

export default GenTable; 